//
//  BGHomeWebViewController.m
//  bigMall
//
//  Created by LTOVE on 2017/1/23.
//  Copyright © 2017年 thinklev. All rights reserved.
//

#import "BGHomeWebViewController.h"
#import "UIViewController+BackButtonHandler.h"
#import "BGLoginViewController.h"
#import "MBProgressHUD.h"
#import "MBProgressHUD+MJ.h"

@interface BGHomeWebViewController ()
@property(nonatomic,assign)BOOL isShow;
@end

@implementation BGHomeWebViewController

- (void)caWithNumber:(NSString *)number twoNum:(NSString *)two{
    NSLog(@"%@--%@",number,two);
    if ([number isEqualToString:@"title"]) {
        self.title = two;
//        [self.navigationController pushViewController:[BGLoginViewController new] animated:YES];
    }else if ([number isEqualToString:@"needLogin"]){
        if (self.isShow) {
            [self.view endEditing:YES];
        }
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:[BGLoginViewController new] animated:YES];
//        self.hidesBottomBarWhenPushed = NO;
    }else if ([number isEqualToString:@"toast"]){
        
        if (self.isShow) {
            [self.view endEditing:YES];
        }
        [self showMessageWithTitle:two];
    }else if ([number isEqualToString:@"loading"]){
        [MBProgressHUD showError:@"loading..."];
    }else if ([number isEqualToString:@"close"]){
        [MBProgressHUD hideHUD];
    }

}

- (void)showMessageWithTitle:(NSString *)title{
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:title preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    [alertVc addAction:cancel];
    dispatch_queue_t concurrentQueue = dispatch_queue_create("my.concurrent.queue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(concurrentQueue, ^{
        [self presentViewController:alertVc animated:YES completion:nil];
    });
}

- (BOOL)navigationShouldPopOnBackButton{
    if ([self.webView canGoBack]) {
        [self.webView goBack];
        return NO;
    }else{
        return YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:webView];
    self.webView = webView;
    self.webView.backgroundColor = [UIColor clearColor];
    self.webView.opaque = NO;
    self.webView.delegate = self;
    self.webView.scrollView.bounces = NO;
//    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.66:8888/api_1/teamBuy/teamlist"]];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
    [self.webView loadRequest:request];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardDismiss:) name:UIKeyboardDidHideNotification object:nil];
    // Do any additional setup after loading the view.
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyBoardDismiss:(NSNotification *)noti{
    self.isShow = YES;
}
- (void)keyBoardShow:(NSNotification *)noti{
    self.isShow = NO;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSString *pageTitle = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
//    NSLog(@"%@aaaa",pageTitle);
    self.title = pageTitle;
    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//    __weak id myself = self;
    self.context[@"ios"] = self;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSMutableURLRequest *mutableRequest = [request mutableCopy];
    
    NSString *sendParam = [[NSString alloc] initWithData:mutableRequest.HTTPBody encoding:NSUTF8StringEncoding];
    
    if(![sendParam isEqualToString:@""]){
        
//        NSString *newData = [sendParam MD5Value];
        
//        NSUInteger length = [newData length];
        NSString *length = [[NSUserDefaults standardUserDefaults] objectForKey:sessionId];
        [mutableRequest addValue:[NSString stringWithFormat:@"%@",length] forHTTPHeaderField:@"PHPSESSID"];
        
        request = [mutableRequest copy];
        
    }else{
        
        return YES;
    }
    
    [self.webView loadRequest:request];
    
    return YES;
}
@end
